package designprinciples.opc;


import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName NoOCP
 * @Description: 没有遵循OPC原则
 * 原始设计：在TPS到达一定数量或者error数到达一定数量时触发报警
 * @Author jiajun
 **/
public class NoOCP {
    // tps报警
    public static void main(String[] args) {
        AlertRule alertRule = new AlertRule();
        Alert alert = new Alert(new AlertRule(), new Notification());
        alert.check("/test", 2000, 5000, 5);
    }
}

class Alert {
    private AlertRule rule;
    private Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    public void check(String api, long requestCount, long errorCount, long durationOfSeconds) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "tps");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE, "error");
        }
    }
}

class AlertRule {
    private static Map<String, RuleData> ruleDataMap;

    static {
        ruleDataMap = new HashMap<>();
        RuleData ruleData = new RuleData();
        ruleData.setMaxErrorCount(4000);
        ruleData.setMaxTimeout(150);
        ruleData.setMaxTps(150);

        ruleDataMap.put("/test", ruleData);
    }

    public RuleData getMatchedRule(String api) {
        return ruleDataMap.get(api);
    }

    public static class RuleData {

        private int maxTimeout;

        private int maxTps;

        private int maxErrorCount;

        public int getMaxTimeout() {
            return maxTimeout;
        }

        public void setMaxTimeout(int maxTimeout) {
            this.maxTimeout = maxTimeout;
        }

        public int getMaxTps() {
            return maxTps;
        }

        public void setMaxTps(int maxTps) {
            this.maxTps = maxTps;
        }

        public int getMaxErrorCount() {
            return maxErrorCount;
        }

        public void setMaxErrorCount(int maxErrorCount) {
            this.maxErrorCount = maxErrorCount;
        }
    }
}

class ApiStatInfo {

    private String api;
    private long requestCount;
    private long errorCount;
    private long durationOfSeconds;
    private long timeoutCount; // 改动一：添加新字段

    public String getApi() {
        return api;
    }

    public void setApi(String api) {
        this.api = api;
    }

    public long getRequestCount() {
        return requestCount;
    }

    public void setRequestCount(long requestCount) {
        this.requestCount = requestCount;
    }

    public long getErrorCount() {
        return errorCount;
    }

    public void setErrorCount(long errorCount) {
        this.errorCount = errorCount;
    }

    public long getDurationOfSeconds() {
        return durationOfSeconds;
    }

    public void setDurationOfSeconds(long durationOfSeconds) {
        this.durationOfSeconds = durationOfSeconds;
    }

    public long getTimeoutCount() {
        return timeoutCount;
    }

    public void setTimeoutCount(long timeoutCount) {
        this.timeoutCount = timeoutCount;
    }
}

class Notification {

    public void notify(NotificationEmergencyLevel level, String s) {
        System.out.printf("urgent!!!  level:{%s}, message:{%s} \n", level.getName(), s);
    }

}

enum NotificationEmergencyLevel {

    TRIVIAL("无关紧要"),
    NORMAL("普通"),
    URGENCY("紧急"),
    SEVERE("严重");

    private String name;

    NotificationEmergencyLevel(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}